﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Web.Services;
using System.Data;

public partial class ImageVideosUpload : System.Web.UI.Page
{
    //string previousFiles = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lstFilePath.Items.Clear();
            lstVideoPath.Items.Clear();
            uploadPhotos();

            if (Request.QueryString["EditListingId"] != null)
                loadPreviousListingImages();
        }
    }

    private void loadPreviousListingImages()
    {
        DataTable dtListingImages = new DataTable();
        dtListingImages = BuddyAgentDAL.GetListingAllPhotos(Convert.ToInt32(Request.QueryString["EditListingId"]));
        for (int i = 0; i < dtListingImages.Rows.Count; i++)
            lstFilePath.Items.Add(dtListingImages.Rows[i][1].ToString());

        PhotoPlaceholder.Controls.Clear();
        foreach (ListItem li in lstFilePath.Items)
        {
            Image imgPhoto = new Image();
            imgPhoto.ID = "img" + li.Text;
            imgPhoto.EnableViewState = true;
            imgPhoto.Width = 80;
            imgPhoto.Height = 80;
            imgPhoto.BorderWidth = 5;
            imgPhoto.ImageUrl = li.Text;
            imgPhoto.Style.Add("margin-left", "5px");
            imgPhoto.Style.Add("margin-bottom", "5px");
            imgPhoto.Style.Add("border-color", "DarkBlue");

            imgPhoto.Attributes.Add("onclick", "Javascript:OnImageClick(this);");

            PhotoPlaceholder.Controls.Add(imgPhoto);
        }

    }

    protected void linkPhotoUpload_Click(object sender, EventArgs e)
    {
        uploadPhotos();
    }

    protected void linkVideoUpload_Click(object sender, EventArgs e)
    {
        uploadVideos();
    }

    private void uploadPhotos()
    {
        imgPhotoTick.Visible = true;
        linkPhotoUpload.ForeColor = System.Drawing.Color.Brown;
        //imgVideoTick.Visible = false;
        //linkVideoUpload.ForeColor = System.Drawing.Color.DarkBlue;

        //VideoPanel.Visible = false;
        PhotoPanel.Visible = true;
    }

    private void uploadVideos()
    {
        imgPhotoTick.Visible = false;
        linkPhotoUpload.ForeColor = System.Drawing.Color.DarkBlue;
        //imgVideoTick.Visible = true;
        //linkVideoUpload.ForeColor = System.Drawing.Color.Brown;

        PhotoPanel.Visible = false;
        //VideoPanel.Visible = true;
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string filePath;
        string listingId;
        if (Request.QueryString["EditListingId"] == null)
        {

            listingId = Request.QueryString["ListingId"].ToString();
        }
        else
        {
            listingId = Request.QueryString["EditListingId"].ToString();
        }

        if (UploadImage.PostedFile != null && UploadImage.HasFile)
        {
            string NewDir = Server.MapPath(@"Image\listingId-" + listingId);
            MakeDirectoryIfNotExists(NewDir);

            filePath = @"Image\listingId-" + listingId + @"\" + System.IO.Path.GetRandomFileName().Replace(".", "-") + System.IO.Path.GetExtension(UploadImage.FileName);

            lstFilePath.Items.Add(new ListItem(filePath.Replace(@"\", "/"), "1"));
            UploadImage.SaveAs(Server.MapPath(filePath));
        }
    }

    private void MakeDirectoryIfNotExists(string NewDirectory)
    {
        try
        {
            // Check if directory exists
            if (!Directory.Exists(NewDirectory))
            {
                // Create the directory.
                Directory.CreateDirectory(NewDirectory);
            }
        }
        catch (IOException _ex)
        {
            Response.Write(_ex.Message);
        }
    }

    protected void btnAddSelected_Click(object sender, EventArgs e)
    {
        string listingId;
        if (Request.QueryString["EditListingId"] == null)
        {
            listingId = Request.QueryString["ListingId"].ToString();

        }
        else
        {
            listingId = Request.QueryString["EditListingId"].ToString();
            BuddyAgentDAL.deleteAllListingPhotos(listingId);
        }


        PhotoPlaceholder_PreRender(PhotoPlaceholder, e);
        int j = 0;
        string filePath = "";
        foreach (ListItem li in lstFilePath.Items)
        {
            string s = desSlectedFiles.Value;

            if (s.Contains(li.Text))
            {
                //No need to save in DB
                string[] str = li.Text.Split('/');
                filePath = Server.MapPath(@"Image\listingId-" + listingId + @"\" + str[2]);
                FileInfo TheFile = new FileInfo(filePath);
                if (TheFile.Exists)
                {
                    File.Delete(filePath);
                }
            }
            else
            {
                BuddyAgentDAL.setListingImages(Convert.ToInt32(listingId), li.Text);
            }
        }

        //string listingId = Request.QueryString["ListingId"].ToString();
        //string NewDir = Server.MapPath(@"Image\listingId-" + listingId);

        //string sourceDirectory = Server.MapPath(@"Image\UserImages");
        //string targetDirectory = Server.MapPath(@"Image\listingId-" + listingId);
        //Copy(sourceDirectory, targetDirectory);

        Page.ClientScript.RegisterStartupScript(this.GetType(), "myCloseScript", "window.close()", true);
        //Response.Redirect("Default.aspx");
    }

    protected void PhotoPlaceholder_PreRender(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            if (PhotoPlaceholder.Controls.Count == 0)
            {
                foreach (ListItem li in lstFilePath.Items)
                {
                    Image imgPhoto = new Image();
                    imgPhoto.ID = "img" + li.Text;
                    imgPhoto.EnableViewState = true;
                    imgPhoto.Width = 80;
                    imgPhoto.Height = 80;
                    imgPhoto.BorderWidth = 5;
                    imgPhoto.ImageUrl = li.Text;
                    imgPhoto.Style.Add("margin-left", "5px");
                    imgPhoto.Style.Add("margin-bottom", "5px");
                    imgPhoto.Style.Add("border-color", "DarkBlue");

                    string s = desSlectedFiles.Value;

                    if (s.Contains(li.Text))
                    {
                        imgPhoto.Style.Add("border-color", "White");
                    }

                    imgPhoto.Attributes.Add("onclick", "Javascript:OnImageClick(this);");

                    PhotoPlaceholder.Controls.Add(imgPhoto);
                }
            }
        }
    }

    //protected void VideoPlaceholder_PreRender(object sender, EventArgs e)
    //{
    //    if (Page.IsPostBack)
    //    {
    //        if (VideoPlaceholder.Controls.Count == 0)
    //        {
    //            foreach (ListItem li in lstVideoPath.Items)
    //            {
    //                LinkButton LinkShowYouTubeVideo = new LinkButton();
    //                LinkShowYouTubeVideo.ID = "link" + li.Text;
    //                LinkShowYouTubeVideo.Width = 80;
    //                LinkShowYouTubeVideo.Height = 80;
    //                LinkShowYouTubeVideo.BorderWidth = 5;

    //                LinkShowYouTubeVideo.Style.Add("margin-left", "5px");
    //                LinkShowYouTubeVideo.Style.Add("margin-bottom", "5px");
    //                LinkShowYouTubeVideo.Style.Add("border-color", "DarkBlue");

    //                LinkShowYouTubeVideo.Text = "<object width='80' height='80'><param name='movie' value='http://www.youtube.com/v/" + li.Text + "'>"+
    //                "</param><param name='wmode' value='transparent'></param><embed src='http://www.youtube.com/v/" + li.Text + "'" +
    //                " type='application/x-shockwave-flash' wmode='transparent' width='100' height='100'></embed></object>";

    //                //string s = desSlectedFiles.Value;

    //                //if (s.Contains(li.Text))
    //                //{
    //                //    imgPhoto.Style.Add("border-color", "White");
    //                //}

    //                LinkShowYouTubeVideo.Attributes.Add("onclick", "Javascript:OnVideoClick(this);");

    //                VideoPlaceholder.Controls.Add(LinkShowYouTubeVideo);
    //            }
    //        }
    //    }
    //}

    //protected void btnLoad_Click(object sender, EventArgs e)
    //{
    //    string[] videoPath = txtVideoPath.Text.Split('?');

    //    string VideoID = videoPath[1].Split('=')[1];


    //    if (VideoID != null)
    //    {
    //        lstVideoPath.Items.Add(new ListItem(VideoID, "1"));
    //        txtVideoPath.Text = "";
    //    }
    //    else
    //    {

    //    }
    //}
}